package com.junzhiit.nengji.cms.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.junzhiit.nengji.bo.QuestionBO;
import com.junzhiit.nengji.cms.service.QuestionService;
import com.junzhiit.nengji.cms.web.exception.QuestionNotFoundException;
import com.junzhiit.nengji.cms.web.util.ViewObjectUtil;
import com.junzhiit.nengji.cms.web.vo.DataTablesParamVO;
import com.junzhiit.nengji.cms.web.vo.QuestionAddVO;
import com.junzhiit.nengji.cms.web.vo.QuestionEditVO;
import com.junzhiit.nengji.cms.web.vo.QuestionVO;
import com.junzhiit.nengji.cms.web.vo.ResultVO;
import com.junzhiit.nengji.dao.QuestionDAO;
import com.junzhiit.nengji.model.Question;

@Service
public class QuestionServiceImpl extends BaseServiceImpl implements QuestionService {
	
	@Autowired
	private QuestionDAO questionDAO;

	@Override
	public Map<String, Object> findAll(DataTablesParamVO dtp,String questionTypeId) {
		List<QuestionBO> list = questionDAO.findAll(dtp.getStart(), dtp.getLength(),questionTypeId);

		List<QuestionVO> listVO = new ArrayList<QuestionVO>();
		for (QuestionBO advertiseBO : list) {
			QuestionVO vo = new QuestionVO();
			BeanUtils.copyProperties(advertiseBO, vo);
			listVO.add(vo);
		}
		
		int totalCount = questionDAO.getTotalCount(questionTypeId);
		return ViewObjectUtil.dataTables(listVO, dtp.getDraw(), totalCount);
	}

	@Override
	public ResultVO add(QuestionAddVO questionAddVO) {
		ResultVO result = new ResultVO();
		Question question = new Question();
		BeanUtils.copyProperties(questionAddVO, question);
		Question param =questionDAO.insert(question);
		QuestionVO vo = new QuestionVO();
		BeanUtils.copyProperties(param, vo);
		result.setData(vo);
		return result;
	}

	@Override
	public ResultVO edit(QuestionEditVO questionEditVO) {
		ResultVO resultVO = new ResultVO();
		Question question = questionDAO.query(questionEditVO.getId());
		
		if (question == null) {
			throw new QuestionNotFoundException("该问题不存在！");
		}
		
		BeanUtils.copyProperties(questionEditVO, question);
		question.setUpdatedTime(new Date());
		resultVO.setData(questionDAO.update(question));
		return resultVO;
	}

	@Override
	public ResultVO delele(String id) {
		ResultVO resultVO = new ResultVO();
		try {
			resultVO.setData(questionDAO.updateToDelete(id));
		} catch (Exception e) {
			throw new QuestionNotFoundException("该问题不存在！");
		}
		return resultVO;
	}

	@Override
	public ResultVO findById(String id) {
		ResultVO resultVO = new ResultVO();
		QuestionBO questionBO = questionDAO.findById(id);
		if (questionBO == null) {
			throw new QuestionNotFoundException("该问题不存在！");
		}
		QuestionVO vo = new QuestionVO();
		BeanUtils.copyProperties(questionBO, vo);
		resultVO.setData(vo);
		return resultVO;
	}

	@Override
	public ResultVO toTop(String id) {
		ResultVO resultVO = new ResultVO();
		
		Question question = questionDAO.query(id);
		if (question == null) {
			throw new QuestionNotFoundException("该问题不存在！");
		}
		question.setSort(questionDAO.getMaxSort() + 1);
		question.setUpdatedTime(new Date());
		
		resultVO.setData(questionDAO.update(question));
		return resultVO;
	}
	
}
